home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swags_z.zip / SORTING.SWG / 0046_Re: Sorting an Array of.pas < prev    next >
Pascal/Delphi Source File  |  1994-05-25  |  3KB  |  115 lines

  1. (*
  2.  -=> Quoting Tim Benoit to All <=-
  3.  
  4.  TB> I am having a bit of difficulty figuring out how to sort an
  5.  TB> array of records by numerical or alphabetical order.
  6.  TB>                  I need all the information that goes with that
  7.  TB> specific Record to stay with it...
  8.  
  9.     This example uses a modified bubblesort algorithm, not real fast but
  10. fairly easy to follow. You may want to use a faster sort procedure but
  11. the basic idea is to examine the data in your selected sort
  12. field (RecArray[i].variable) but do the sort on the whole
  13. record (RecArray[i]):
  14.  
  15. eg:
  16.      Var
  17.  
  18.          Buffer : Rec;
  19.  
  20.      If RecArray[3].Number1 > RecArray[4].Number1 then { sort }
  21.  
  22.         Begin   { interchange RecArray[3] and RecArray[4] }
  23.                 Buffer := RecArray[3];
  24.                 RecArray[3] := RecArray[4];
  25.                 RecArray[4] := Buffer
  26.         End;
  27.  
  28. Bubblesort makes multiple passes moving data only one place per pass.
  29. This example is similar but uses only one pass.
  30. *)
  31.  
  32. Program Modsort;
  33.  
  34. uses Crt;      {only needed for clrscr}
  35.  
  36. Const
  37.   max = 10;      {max number of records}
  38.  
  39. Type
  40.   fieldtype = string[2];
  41.   datatype = record
  42.          rec1 : fieldtype;
  43.          rec2 : fieldtype;
  44.          end;
  45. Var
  46.   data : array [1..max] of datatype;
  47.   i,j : byte;
  48.  
  49. Procedure interchange(r,l:datatype);
  50.  
  51. Var
  52.    buffer : datatype;
  53.  
  54. Begin
  55.      buffer := r;
  56.      data[i] := l;
  57.      data[i+1] := buffer;
  58.      dec(i);
  59. End;
  60.  
  61. Procedure sort(j : byte);  {j is the selected sort field number}
  62.  
  63. Var
  64.    field : array [1..2] of fieldtype;
  65.  
  66. Begin
  67.    i := 1;
  68.  
  69.    While i < max  do
  70.          Begin
  71.             Case j of
  72.                 1 : Begin
  73.                        field[1] := data[i].rec1;
  74.                        field[2] := data[i+1].rec1;
  75.                     End;
  76.                 2 : Begin
  77.                        field[1] := data[i].rec2;
  78.                        field[2] := data[i+1].rec2;
  79.                     End;
  80.             End;
  81.  
  82.          If field[1] > field[2] then
  83.             Interchange(data[i], data[i+1])
  84.          Else
  85.             Inc(i);
  86.          End;
  87. End;
  88.  
  89. Begin                              {main}
  90.  
  91. Clrscr;
  92. Writeln('UNSORTED :');
  93. For i := 1 to max do              {make up random array of alphas}
  94.     Begin
  95.         j := random(26);
  96.         data[i].rec1 := chr(j+65);
  97.         Write(data[i].rec1);
  98.         j := random(26);
  99.         Data[i].rec2 := chr(j+65);
  100.         Writeln(',',data[i].rec2);
  101.     End;
  102.  
  103. Write('Sort on which field? ');
  104. Readln(j);
  105. Sort(j);
  106. Writeln('SORTED ON FIELD: ',j);
  107.  
  108. For i := 1 to max do
  109.     Begin
  110.         Write(data[i].rec1);
  111.         Writeln(',',data[i].rec2);
  112.     End;
  113.  
  114. End.
  115.